package cn.edu.zjicm.wordsnet_d.view;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.RechargeActivity;
import cn.edu.zjicm.wordsnet_d.activity.SelectBookActivity;
import cn.edu.zjicm.wordsnet_d.data.MobclickAgentData;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.synchronize.PayInterface;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;
import cn.edu.zjicm.wordsnet_d.util.Utils;

public class PayDialog {

	private Context mContext;
	private Dialog dialog;
	private View view;
	private TextView titleTv, priceTv, balanceTv;
	private Button getBtn, cancelBtn;
	private int bookId;
	private int balance, price;

	public PayDialog(Activity context, int bookId) {
		this.mContext = context;
		this.bookId = bookId;
		findView();
		initView();
		clickFunction();
	}

	private void findView() {
		view = LayoutInflater.from(mContext).inflate(R.layout.view_pay_dialog, null);
		titleTv = (TextView) view.findViewById(R.id.pay_dialog_title);
		priceTv = (TextView) view.findViewById(R.id.pay_dialog_price);
		balanceTv = (TextView) view.findViewById(R.id.pay_dialog_balance);
		getBtn = (Button) view.findViewById(R.id.pay_dialog_get_btn);
		cancelBtn = (Button) view.findViewById(R.id.pay_dialog_cancel);
	}

	private void initView() {
		price = WordFactory.getInstance(mContext).getPrice(bookId);
		titleTv.setText(WordFactory.getInstance(mContext).getBookName(AppPreference.Book.BOOK_ID[bookId]));
		priceTv.setText(price + "知米豆");
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.cancel();
				MobclickAgentData.reCharge(mContext, "弹出窗口，点击取消按钮");
			}
		});
		resetBalance();
		getBtn.setOnClickListener(onItemClickListener);
	}

	public void resetBalance() {
		balance = WordFactory.getInstance(mContext).getZMD();
		if (balance >= price) {
			balanceTv.setText("您已有" + balance + "个知米豆，点击“立即获取”继续学习哦");
			getBtn.setText("立即获取");
		} else {
			balanceTv.setText("您已有" + balance + "个知米豆，还需要" + (price - balance) + "个知米豆才能学习哦");
			getBtn.setText("充值");
		}
	}

	OnClickListener onItemClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (getBtn.getText().equals("立即获取")) {
				if (bookId == 20 || bookId == 21) {
					doBuyWithZMD((bookId - 2) + "");
				} else if (bookId == 102) { // 四级单词+词组
					doBuyWithZMD(101 + "");
				
				} else if (bookId == 107) { // 六级单词+词组
					doBuyWithZMD(106 + "");
				
				} else if (bookId == 105) { // 考研单词+词组
					doBuyWithZMD(103 + "");
				
				} else if (bookId == 109) { // 雅思单词+词组
					doBuyWithZMD(108 + "");
				
				} else if (bookId == 111) { // 托福单词+词组
					doBuyWithZMD(110 + "");
				
				} else {
					doBuyWithZMD(bookId + "");
				}
			} else if (getBtn.getText().equals("充值")) {
				mContext.startActivity(new Intent(mContext, RechargeActivity.class));
				MobclickAgentData.reCharge(mContext, "弹出窗口，点击充值按钮");
			}
		}
	};

	void doBuyWithZMD(String productID) {
		buyWithZMDHandler.sendMessage(buyWithZMDHandler.obtainMessage(UserInterface.MSG_START));
		PayInterface.getInstance(mContext).buyWithZmd(productID, buyWithZMDHandler);
	}

	Handler buyWithZMDHandler = new Handler() {
		ProgressDialog dialog2 = null;

		public void handleMessage(android.os.Message msg) {
			if (msg.what != UserInterface.MSG_START)
				if (dialog2 != null) {
					dialog2.dismiss();
					dialog2 = null;
				}
			switch (msg.what) {
			case UserInterface.MSG_START:
				dialog2 = ProgressDialog.show(mContext, null, "正在连接服务器");
				break;
			case UserInterface.MSG_SUCCESS:
				PayInterface.getInstance(mContext).getUserWealth();
				Toast.makeText(mContext, "获取成功", Toast.LENGTH_SHORT).show();
				if (SelectBookActivity.mInstance != null)
					SelectBookActivity.mInstance.toFinish2();
				break;
			case UserInterface.MSG_UNSUCCESS:
				Toast.makeText(mContext, "获取失败，请稍后再试", Toast.LENGTH_SHORT).show();
				dialog.cancel();
				break;
			case UserInterface.MSG_WEB_SITE_ERROR:
				Toast.makeText(mContext, "连接服务器失败，请稍后再试", Toast.LENGTH_SHORT).show();
				dialog.cancel();
				break;
			case UserInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				Toast.makeText(mContext, "请确认网络是否连接", Toast.LENGTH_SHORT).show();
				dialog.cancel();
				break;
			case UserInterface.MSG_BAD_NETWORK:
				Toast.makeText(mContext, "网络未连接，请稍后再试", Toast.LENGTH_SHORT).show();
				dialog.cancel();
				break;
			}
		};
	};

	public void clickFunction() {
		dialog = new Dialog(mContext, R.style.animation_for_share_dialog);// 这里应用了你的自定义样式
		dialog.setCanceledOnTouchOutside(true);
		view.setMinimumWidth(Utils.getScreenWidth(mContext));
		WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
		layoutParams.x = 0;
		final int cMakeBottom = -1000;
		layoutParams.y = cMakeBottom;
		layoutParams.gravity = Gravity.BOTTOM;
		dialog.onWindowAttributesChanged(layoutParams);
		dialog.setContentView(view);
		dialog.show();
	}

	public void cancel() {
		dialog.cancel();
	}
}
